import { _decorator, Component, director, easing, Label, Node, ProgressBar, tween } from 'cc';
import { userinfo } from '../../UserInfo';
import { Utils } from '../../utils/Utils';
import { headComp } from '../mainui/headComp';
import { http } from '../../common/HTTP';
import { lobby } from '../../lobby';
import { showAlert } from '../alert/alert';
import { GameEvent } from '../../GameEvent';
import protocol from 'db://assets/proto/protoExt';
import { new_world } from 'db://assets/proto/protocol';
const { ccclass, property } = _decorator;

@ccclass('fishInfoComp')
export class fishInfoComp extends Component {
    @property(Node)
    close!: Node;

    @property(headComp)
    head!: headComp;

    @property(Label)
    nameLabel!: Label;

    @property(Label)
    genderLabel!: Label;

    @property(Label)
    ageLabel!: Label;

    @property(Label)
    tianfuLabel!: Label;

    @property(Label)
    xinggeLabel!: Label;

    @property(Label)
    linggenLabel!: Label;

    @property(Label)
    tezhiLabel!: Label;

    @property(Label)
    levelLabel!: Label;

    @property(ProgressBar)
    hpProgressBar!: ProgressBar;

    @property(ProgressBar)
    lingliProgressBar!: ProgressBar;

    @property(ProgressBar)
    expProgressBar!: ProgressBar;

    @property(Label)
    attackLabel!: Label;

    @property(Label)
    defenseLabel!: Label;

    @property(Label)
    selfhealing!: Label;

    @property(Label)
    criticalAttack!: Label;

    @property(Label)
    dodge!: Label;

    @property(Label)
    xina!: Label;

    @property(Label)
    xiulian!: Label;

    @property(Node)
    btnLianhua!: Node;

    @property(Node)
    btnjihuo!: Node;

    @property(Node)
    chenshui!: Node;

    fish!: protocol.new_world.shared.v1.IFreshImmortal;
    start() {
        this.onEvent();
    }
    onEvent() {
        this.close.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
        })
        this.btnLianhua.on("click", () => {
            showAlert("炼化", "您确定要炼化这个弟子吗？", async (isok) => {
                if (isok) {
                    let data = await http.requestAsync("POST", "v1/fish/refining", { fish_id: this.fish.id }).catch(() => { lobby.instance.showToast("炼化失败") })
                    if (!data) return
                    userinfo.removeFishByid(this.fish.id)
                    this.node.destroy()
                }
            })
        })

        this.chenshui.on("click", () => {
            showAlert("genshin", "您确定要沉睡这个弟子吗？", async (isok) => {
                if (isok) {
                    let data = await http.requestAsync("POST", "v1/fish/sleep", { fish_id: this.fish.id }).catch(() => { lobby.instance.showToast("沉睡失败") })
                    if (!data) return
                    let fish = userinfo.getFish(this.fish.id);
                    fish.fish.status = new_world.shared.v1.FishStatus.sleep;
                    userinfo.updateFish(fish)
                }
            })
        })
        this.btnjihuo.on("click", () => {
            showAlert("genshin", "您确定要激活这个弟子吗？", async (isok) => {
                if (isok) {
                    let data = await http.requestAsync("POST", "v1/fish/alive", { fish_id: this.fish.id }).catch(() => { lobby.instance.showToast("沉睡失败") })
                    if (!data) return
                    let fish = userinfo.getFish(this.fish.id);
                    fish.fish.status = new_world.shared.v1.FishStatus.alive;
                    userinfo.updateFish(fish)
                }
            })
        })
        director.on(GameEvent.updateFish, this.updateFish, this)
        director.on(GameEvent.removeFish, this.removeFish, this)
    }
    updateFish(fish: protocol.new_world.shared.v1.IImmortalDetail) {
        if (fish.fish.id == this.fish.id) {
            this.UpdateUI(this.fish.id)
        }
    }
    removeFish(fish: protocol.new_world.shared.v1.IImmortalDetail) {
        if (this.fish.id == fish.fish.id) {
            this.node.destroy()
        }
    }
    getGrowthRequireMoney(currentLevel: number): number {
        const exponent = Math.floor(currentLevel / 10);
        return 800 * Math.pow(currentLevel, 3) * Math.pow(1.65, exponent);
    }
    protected onDestroy(): void {
        director.off(GameEvent.updateFish, this.updateFish, this)
        director.off(GameEvent.removeFish, () => { }, this)
    }
    UpdateUI(id: string) {
        this.head.setHead(id)
        let fish: protocol.new_world.shared.v1.IFreshImmortal = userinfo.getFish(id).fish!;
        this.fish = fish;
        console.log(fish)
        this.nameLabel.string = fish.base_info.name;
        this.genderLabel.string = fish.base_info.gender == 1 ? "男" : "女";
        this.ageLabel.string = Math.ceil((Utils.getServerTime() - (fish.base_info.created_at_ms as number)) / 1000 / 60 / 60 / 2) + "岁"
        this.tianfuLabel.string = Utils.getQualityStr(fish.base_info.quality);
        this.xinggeLabel.string = fish.personality_id + ""
        this.linggenLabel.string = Utils.getlinggenStr(fish.ling_gen_skills)
        // this.tezhiLabel.string = fish.
        this.levelLabel.string = Utils.getLevelStr(fish.asset_info.level)
        tween(this.hpProgressBar).to(0.5, { progress: fish.battle_info.heal as number / (fish.battle_info.max_heal as number) }, { easing: "circOut" }).start()
        tween(this.lingliProgressBar).to(0.5, { progress: fish.battle_info.energy as number / (fish.battle_info.max_energy as number) }, { easing: "circOut" }).start()
        tween(this.expProgressBar).to(0.5, { progress: fish.asset_info.money as number / (this.getGrowthRequireMoney(fish.asset_info.level as number)) }, { easing: "circOut" }).start()

        this.hpProgressBar.node.getChildByName("hpLabel").getComponent(Label).string = fish.battle_info.heal + "/" + fish.battle_info.max_heal;
        this.lingliProgressBar.node.getChildByName("lingliLabel").getComponent(Label).string = fish.battle_info.energy + "/" + fish.battle_info.max_energy;
        this.expProgressBar.node.getChildByName("expLabel").getComponent(Label).string = fish.asset_info.money + "/" + (this.getGrowthRequireMoney(fish.asset_info.level as number));
        

        this.attackLabel.string = fish.battle_info.atk + ""
        this.attackLabel.node.parent.active = !!fish.battle_info.atk;

        this.defenseLabel.string = fish.battle_info.def + ""
        this.defenseLabel.node.parent.active = !!fish.battle_info.def;

        this.selfhealing.string = fish.battle_info.recover_speed + ""
        this.selfhealing.node.parent.active = !!fish.battle_info.recover_speed;

        this.criticalAttack.string = fish.battle_info.ct + ""
        this.criticalAttack.node.parent.active = !!fish.battle_info.ct;

        this.dodge.string = fish.battle_info.dodge + ""
        this.dodge.node.parent.active = !!fish.battle_info.dodge;

        this.xina.string = fish.battle_info.absorb + ""
        this.xina.node.parent.active = !!fish.battle_info.absorb;

        this.xiulian.string = fish.battle_info.upgrade_speed + ''
        this.xiulian.node.parent.active = !!fish.battle_info.upgrade_speed;

        this.btnLianhua.active = fish.status == new_world.shared.v1.FishStatus.dead;
        this.btnjihuo.active = fish.status == new_world.shared.v1.FishStatus.sleep;
        this.chenshui.active = (fish.status == new_world.shared.v1.FishStatus.alive) || !fish.status;
    }
    update(deltaTime: number) {

    }

}


